<?php

namespace app\modules\user\services;

use app\common\services\BaseService;
use app\config\Constants;
use app\models\User;
use app\models\UserCredential;
use app\models\UserWechatInfo;
use yii\base\UserException;
use yii\validators\UrlValidator;

class UserService extends BaseService{

    /**
     * 创建
     * @param $data
     * @param $credential_type
     * @return User
     */
    public static function create($data, $credential_type){
        $user = null;
        if($credential_type == Constants::WECHAT){
            $user = self::createByWechat($data);
        }else{
            $user = self::createByMobile($data);
        }

        if(!$user){
            throw new UserException(\Yii::t("用户创建失败"));
        }

        return $user;
    }

    /**
     * 手机方式
     * @param $mobile
     * @return User|null
     * @throws \Throwable
     * @throws \yii\db\Exception
     */
    protected static function createByMobile($mobile){
        $transaction = \Yii::$app->getDb()->beginTransaction();

        $user = new User();
        $user->setAttribute("mobile",$mobile);
        $user->setAttribute("add_time",date("Y-m-d H:i:s"));
        if($user->insert() === false){
            $transaction->rollBack();
            return null;
        }

        $user_credential = new UserCredential();
        $user_credential->setAttribute("user_id",$user->user_id);
        $user_credential->setAttribute("username",$mobile);
        $user_credential->setAttribute("type",Constants::PHONE);
        if($user_credential->insert() === false){
            $transaction->rollBack();
            return null;
        }

        $transaction->commit();
        return $user;
    }

    /**
     * 微信方式
     * @param $user_info
     * @return User
     * @throws \Throwable
     */
    protected static function createByWechat($user_info){
        $transaction = \Yii::$app->getDb()->beginTransaction();

        $user = new User();
        $user->setAttribute("username",$user_info['nickName']);
        $user->setAttribute("thumbnail",$user_info['avatarUrl']);
        $user->setAttribute("add_time",date("Y-m-d H:i:s"));
        if($user->insert() === false){
            $transaction->rollBack();
            return null;
        }

        $user_wechat_info = new UserWechatInfo();
        $user_wechat_info->setAttribute("user_id",$user->user_id);
        $user_wechat_info->setAttribute("openid",$user_info['openId']);
        $user_wechat_info->setAttribute("avatar_url",$user_info['avatarUrl']);
        $user_wechat_info->setAttribute("country",$user_info['country']);
        $user_wechat_info->setAttribute("city",$user_info['city']);
        $user_wechat_info->setAttribute("gender",$user_info['gender']);
        $user_wechat_info->setAttribute("nickname",$user_info['nickName']);
        $user_wechat_info->setAttribute("province",$user_info['province']);
        if($user_wechat_info->insert() === false){
            $transaction->rollBack();
            return null;
        }

        $user_credential = new UserCredential();
        $user_credential->setAttribute("user_id",$user->user_id);
        $user_credential->setAttribute("username",$user_info['openId']);
        $user_credential->setAttribute("type",Constants::WECHAT);
        if($user_credential->insert() === false){
            $transaction->rollBack();
            return null;
        }

        $transaction->commit();
        return $user;
    }

    /**
     * 获取信息
     * @param $credential
     * @param $type
     * @return null|User
     */
    public static function findInfoByCredential($credential,$type){
        $credential = UserCredential::findOne(['username'=>$credential,'type'=>$type]);
        return $credential ? $credential->user : null;
    }

    /**
     * 更新头像
     * @param $id
     * @param $thumbnail
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function updateThumbnail($id,$thumbnail){
        $validator = new UrlValidator();
        if(!$validator->validate($thumbnail)){
            self::setError(\Yii::t("app","地址格式错误"));
            return false;
        }
        $user = User::findOne($id);
        $user->setAttribute("thumbnail",$thumbnail);
        return $user->update() !== false;
    }

    /**
     * 更新用户名
     * @param $id
     * @param $username
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public static function updateUsername($id,$username){
        if(empty($username)){
            self::setError(\Yii::t("app","用户名不能为空"));
            return false;
        }
        $user = User::findOne($id);
        $user->setAttribute("username",$username);
        return $user->update() !== false;
    }
}